package com.yyaccp.appinfo.controller;

import com.yyaccp.appinfo.domain.AppInfo;
import com.yyaccp.appinfo.domain.AppVersion;
import com.yyaccp.appinfo.mapper.AppVersionMapper;
import com.yyaccp.appinfo.service.AppInfoService;
import com.yyaccp.appinfo.utils.AjaxResult;
import com.yyaccp.appinfo.utils.ExcelUtils;
import com.yyaccp.appinfo.utils.PageBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.IOException;
import java.util.List;
import java.util.Objects;

@RestController
@RequestMapping("/api/appInfos")

public class AppInfoController {
    @Autowired
    private AppInfoService appInfoService;

    @Autowired
    private AppVersionMapper appVersionMapper;

    @PostMapping("/add")
    public AjaxResult add(AppInfo appInfo , MultipartFile file1 , HttpServletRequest request) throws IOException {
        String path = request.getServletContext().getRealPath("/files");
        file1.transferTo(new File(path, Objects.requireNonNull(file1.getOriginalFilename())));
        appInfo.setLogoLocPath(path+"\\"+Objects.requireNonNull(file1.getOriginalFilename()));
        if(appInfoService.add(appInfo) > 0){
            return AjaxResult.success(file1.getOriginalFilename());
        } else {
            return AjaxResult.error(file1.getOriginalFilename());
        }
    }

    @RequestMapping("/search")
    public AjaxResult search(AppInfo appInfo, @RequestParam("page") Integer pageNum, @RequestParam("limit") Integer pageSize){
        PageBean pageBean = new PageBean();
        pageBean.setPageNum(pageNum);
        pageBean.setPageSize(pageSize);
        appInfoService.search(appInfo,pageBean);
        return AjaxResult.success(pageBean);
    }



    @RequestMapping("/exportExcel")
    public AjaxResult exportExcel(AppInfo appInfo, HttpServletResponse response) throws Exception {
        try {
            ExcelUtils.export(appInfoService.search(appInfo),response);
        } catch (IOException e) {
            e.printStackTrace();
            return AjaxResult.error(e.getMessage());
        }
        return AjaxResult.success();
    }
    //审核
    @RequestMapping("/upd")
    public AjaxResult upd(AppInfo appInfo){
        return AjaxResult.success(appInfoService.upd(appInfo));
    }
    //yz
    @RequestMapping("/yzName")
    public AjaxResult yzName(String softwareName){
        if(appInfoService.yzName(softwareName) > 0) {
            return AjaxResult.success(200);
        }else {
            return AjaxResult.error(201);
        }
    }

    @RequestMapping("/yzAPK")
    public AjaxResult yzAPK(String APKName){
        if(appInfoService.yzAPK(APKName) > 0) {
            return AjaxResult.success(200);
        }else {
            return AjaxResult.error(201);
        }
    }

    @RequestMapping("/del")
    public AjaxResult del(Long id) throws Exception {
        boolean flag = false;
        int versionCount = appVersionMapper.getVersionCountByAppId(id);
        List<AppVersion> appVersionList = null;
        if(versionCount > 0){
            appVersionList = appVersionMapper.getVersionList(id);
            for(AppVersion appVersion : appVersionList){
                if(appVersion.getApkLocPath()!=null && !appVersion.getApkLocPath().equals("")){
                    File file = new File(appVersion.getApkLocPath());
                    if(file.exists()){
                        if(!file.delete())
                            throw new Exception();
                    }
                }
            }
            appVersionMapper.deleteByPrimaryKey(id);
        }
        AppInfo appInfo = appInfoService.getAppInfo(id);
        if(appInfo.getLogoLocPath()!=null && !appInfo.getLogoLocPath().equals("")){
            File file = new File(appInfo.getLogoLocPath());
            if(file.exists()){
                if(!file.delete())
                    throw new Exception();
            }
        }
        if(appInfoService.delById(id) > 0){
            flag = true;
        }
        return AjaxResult.success(flag);
    }
}
